package com.example.manager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.annotation.Log;
import com.example.common.exception.GuiguException;
import com.example.common.helper.JwtHelper;
import com.example.common.utils.AuthContextUtil;
import com.example.manager.service.SysMenuService;
import com.example.manager.service.SysUserService;
import com.example.manager.service.ValidateCodeService;
import com.example.model.dto.system.LoginDto;
import com.example.model.dto.system.SysUserDto;
import com.example.model.entity.system.SysUser;
import com.example.model.vo.common.Result;
import com.example.model.vo.common.ResultCodeEnum;
import com.example.model.vo.system.LoginVo;
import com.example.model.vo.system.SysMenuVo;
import com.example.model.vo.system.ValidateCodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "生成验证码")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {

        try {
            ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
            return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
        } catch (Exception e) {
            throw new GuiguException(500, "验证码生成失败");
        }
    }

    @ApiOperation(value = "用户退出")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {

        try {
            sysUserService.logout(token);
            return Result.build(null, ResultCodeEnum.SUCCESS);
        } catch (Exception e) {
            throw new GuiguException(500, "用户退出失败");
        }
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {

        String jwtToken = AuthContextUtil.getJwtToken();
        if (jwtToken != null) {
            Long userId = JwtHelper.getUserId(jwtToken);
            SysUser sysUser = sysUserService.getById(userId);
            return Result.build(sysUser, ResultCodeEnum.SUCCESS);
        } else {
            throw new GuiguException(500, "用户未登录");
        }
    }

    //用户登录
    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto) {

        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("分页查询所有用户")
    @GetMapping("/findAllSysUser")
    @ApiImplicitParam(name = "token", value = "token值", dataType = "String", paramType = "header")
    public Result findAllSysUser(SysUserDto sysUserDto) {
        Page<SysUser> data = sysUserService.findAllSysUser(sysUserDto);
        return Result.build(data, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("用户添加或修改")
    @PostMapping("/saveOrUpdateUser")
    @ApiImplicitParam(name = "token", value = "token值", dataType = "String", paramType = "header")
    public Result saveOrUpdateUser(@RequestBody SysUser sysUser) {
        sysUserService.saveOrUpdate(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("用户删除")
    @DeleteMapping("/deleteUser")
    @ApiImplicitParam(name = "token", value = "token值", dataType = "String", paramType = "header")
    public Result deleteBatchUser(@RequestBody List<Long> idList) {
        sysUserService.deleteBatch(idList);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody SysUser sysUser) {
        sysUserService.saveUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("密码修改")
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody SysUser sysUser) {
        sysUserService.updatePassword(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "获取当前登录用户的菜单",businessType = 1)
    @ApiOperation("获取当前登录用户的菜单")
    @GetMapping("/menus")
    @ApiImplicitParam(name = "token", value = "token值", dataType = "String", paramType = "header")
    public Result menus(HttpServletRequest request) {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList(request) ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }

}
